package com.wejoy.video.middle.station.data.service.overtime.impl;

import com.wejoy.video.middle.station.data.enums.dict.EnumStatus;
import com.wejoy.video.middle.station.data.mapper.overtime.IOvertimePointMapper;
import com.wejoy.video.middle.station.data.domain.overtime.OvertimePoint;
import com.wejoy.video.middle.station.data.service.common.IIdWorkerService;
import com.wejoy.video.middle.station.data.service.overtime.IOvertimePointService;
import com.wejoy.video.middle.station.data.util.WJAssert;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 加班点位
 *
 * @author miracle_j
 * @date 2024年04月22日 15:25:48
 */
@Service
public class OvertimePointServiceImpl implements IOvertimePointService {
    @Autowired
    private IOvertimePointMapper overtimePointMapper;
    @Autowired
    private IIdWorkerService idWorkerService;

    @Override
    public void save(OvertimePoint req, String operator) {
        req.setOperator(operator);
        if (null == req.getId()) {
            insertSelective(req);
        } else {
            updateSelectiveById(req);
        }
    }

    @Override
    public int insertSelective(OvertimePoint req) {
        req.setId(idWorkerService.getNextId());
        req.setStatus(EnumStatus.NEW.getCode());
        checkStringLength(req, false);
        return overtimePointMapper.insertSelective(req);
    }

    @Override
    public int updateSelectiveById(OvertimePoint overtimePoint) {
        WJAssert.notNull(overtimePoint, "入参对象不能为空");
        WJAssert.notNull(overtimePoint.getId(), "id参数错误");
        OvertimePoint where = new OvertimePoint();
        where.setId(overtimePoint.getId());
        return overtimePointMapper.updateSelective(overtimePoint, where);
    }

    @Override
    public int getCount(OvertimePoint overtimePoint) {
        checkStringLength(overtimePoint, true);
        return overtimePointMapper.getCount(overtimePoint);
    }

    @Override
    public OvertimePoint getOneById(Long id) {
        WJAssert.notNull(id, "id参数错误");
        OvertimePoint where = new OvertimePoint();
        where.setId(id);
        return overtimePointMapper.getOne(where);
    }

    @Override
    public List<OvertimePoint> getList(OvertimePoint overtimePoint) {
        checkStringLength(overtimePoint, true);
        return overtimePointMapper.getList(overtimePoint);
    }

    @Override
    public List<OvertimePoint> getPageList(OvertimePoint overtimePoint) {
        checkStringLength(overtimePoint, true);
        return overtimePointMapper.getPageList(overtimePoint);
    }

    @Override
    public void audit(Long id, String operator) {
        OvertimePoint req = new OvertimePoint();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.AUDIT.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void unaudit(Long id, String operator) {
        OvertimePoint req = new OvertimePoint();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.NEW.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void delete(Long id, String operator) {
        OvertimePoint req = new OvertimePoint();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.DEL.getCode());
        updateSelectiveById(req);
    }

    /**
     * 参数校验注解默认使用的方法
     */
    private void checkStringLength(OvertimePoint overtimePoint, boolean nullAble) {
        WJAssert.notNull(overtimePoint, "入参对象不能为空");
        WJAssert.limitMaxLength(overtimePoint.getPointConfigEvs(), true, 65535, "evs原点位配置信息不合法!");
        WJAssert.limitMaxLength(overtimePoint.getOperator(), true, 100, "操作者不合法!");
        WJAssert.limitMaxLength(overtimePoint.getRemark(), true, 255, "备注不合法!");
    }
}
